package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.TablePProject;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ITablePProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TablePApplication;
import com.ruoyi.system.service.ITablePApplicationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目立项Controller
 * 
 * @author ruoyi
 * @date 2022-05-17
 */
@Controller
@RequestMapping("/system/application")
public class TablePApplicationController extends BaseController
{
    private String prefix = "system/application";

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ITablePApplicationService tablePApplicationService;

    @Autowired
    private ITablePProjectService tablePProjectService;

    @RequiresPermissions("system:application:view")
    @GetMapping()
    public String application()
    {
        return prefix + "/application";
    }

    /**
     * 查询项目立项列表
     */
    @RequiresPermissions("system:application:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TablePApplication tablePApplication)
    {
        startPage();
        List<TablePApplication> list = tablePApplicationService.selectTablePApplicationList(tablePApplication);
        return getDataTable(list);
    }

    /**
     * 导出项目立项列表
     */
    @RequiresPermissions("system:application:export")
    @Log(title = "项目立项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TablePApplication tablePApplication)
    {
        List<TablePApplication> list = tablePApplicationService.selectTablePApplicationList(tablePApplication);
        ExcelUtil<TablePApplication> util = new ExcelUtil<TablePApplication>(TablePApplication.class);
        return util.exportExcel(list, "项目立项数据");
    }

    /**
     * 新增项目立项
     */
    @GetMapping("/add")
    public String add(ModelMap map)
    {
        //获取用户信息
        map.put("users",userService.selectUserList(new SysUser()));
        return prefix + "/add";
    }

    /**
     * 新增保存项目立项
     */
    @RequiresPermissions("system:application:add")
    @Log(title = "项目立项", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TablePApplication tablePApplication)
    {
        tablePApplication.setPersonId(ShiroUtils.getSysUser().getUserId());
        return toAjax(tablePApplicationService.insertTablePApplication(tablePApplication));
    }

    /**
     * 修改项目立项
     */
    @RequiresPermissions("system:application:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TablePApplication tablePApplication = tablePApplicationService.selectTablePApplicationById(id);
        mmap.put("tablePApplication", tablePApplication);
        mmap.put("users",userService.selectUserList(new SysUser()));
        return prefix + "/edit";
    }

    /**
     * 审批项目立项
     */
    @RequiresPermissions("system:application:approve")
    @GetMapping("/approve/{id}")
    public String approve(@PathVariable("id") Long id, ModelMap mmap)
    {
        TablePApplication tablePApplication = tablePApplicationService.selectTablePApplicationById(id);
        mmap.put("tablePApplication", tablePApplication);
        mmap.put("users",userService.selectUserList(new SysUser()));
        return prefix + "/approve";
    }

    /**
     * 修改保存项目立项
     */
    @RequiresPermissions("system:application:edit")
    @Log(title = "项目立项", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TablePApplication tablePApplication)
    {
        return toAjax(tablePApplicationService.updateTablePApplication(tablePApplication));
    }

    /**
     * 审批保存项目立项
     */
    @RequiresPermissions("system:application:approve")
    @Log(title = "审批项目立项", businessType = BusinessType.UPDATE)
    @PostMapping("/approve")
    @ResponseBody
    public AjaxResult approve(TablePApplication tablePApplication)
    {
        tablePApplication.setApproveId((ShiroUtils.getSysUser().getUserId()));
        tablePApplication.setApproveTime(new Date());
        if(tablePApplication.getApproveStatus() == 1){
            //添加项目信息
            TablePProject tablePProject = new TablePProject();
            tablePProject.setCode(tablePApplication.getCode());
            tablePProject.setName(tablePApplication.getName());
            tablePProject.setApproveStatus(0l);
            tablePProject.setCreateTime(new Date());
            tablePProjectService.insertTablePProject(tablePProject);
        }
        return toAjax(tablePApplicationService.updateTablePApplication(tablePApplication));
    }

    /**
     * 删除项目立项
     */
    @RequiresPermissions("system:application:remove")
    @Log(title = "项目立项", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tablePApplicationService.deleteTablePApplicationByIds(ids));
    }
}
